package org.example.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.CurriculumDto;
import org.example.dto.CurriculumPlanDto;
import org.example.service.CurriculumService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.CurriculumPlanVo;
import org.example.vo.CurriculumVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ronchang Wu
 */
@Controller
public class CurriculumController {
    @Resource
    CurriculumService service;

    /**
     * 获取全部课程
     *
     * @param resp  应答对象
     * @param page  页码
     * @param limit 每页数量
     * @throws IOException
     */
    @GetMapping("/getCurriculum")
    @ResponseBody
    public void getCurriculum(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<CurriculumDto> curriculumList = service.queryAllCurriculum();
        Integer count = service.countCurriculum_();

        String json = "{}";
        ObjectMapper om = new ObjectMapper();
        json = om.writeValueAsString(curriculumList);
        json = "{\"code\":0,\"msg\":\"\",\"count\":" + count + ",\"data\":" + json + "}";
//        System.out.println(json);

        PrintWriter out = resp.getWriter();
        resp.setContentType("application/json;charset=utf-8");
        out.write(json);
        out.flush();
        out.close();
    }

    /**
     * 删除课程
     *
     * @param curriculumId 课程号
     * @return 删除状态
     */
    @GetMapping(value = "/deleteCurriculum", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteCurriculum(String curriculumId) {
        Integer status = 0;
        String[] curriculumIds = curriculumId.split(",");
        for (int i = 0; i < curriculumIds.length; i++) {
            status += service.deleteCurriculum(Integer.parseInt(curriculumIds[i]));
        }
        //以上service根据课程查询id，根据id删除课程，返回结果给status

        return status > 0 ? "success" : "error";
    }

    /**
     * 添加课程
     *
     * @param curriculum 课程信息
     * @return 添加状态
     */
    @GetMapping(value = "/addCurriculum", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addCurriculum(CurriculumVo curriculum) {
        System.out.println(curriculum);
        //调用service给课程赋值id，在调用service添加课程
        Integer status = service.addCurriculum(curriculum);

        return status > 0 ? "success" : "error";
    }

    /**
     * 根据课程号查询对应课程信息
     *
     * @param curriculumId 课程号
     * @return 对应课程信息
     */
    @GetMapping("/getCurriculumById")
    @ResponseBody
    public CurriculumDto getCurriculumById(Integer curriculumId) {
        CurriculumDto curriculum = service.queryCurriculumById_(curriculumId);
        //以上调用service根据curriculumId获取Curriculum

        return curriculum;
    }

    /**
     * 编辑课程
     *
     * @param curriculum 编辑后的课程信息
     * @return 编辑状态
     */
    @GetMapping("/editCurriculum")
    @ResponseBody
    public String editCurriculum(CurriculumVo curriculum) {
        Integer status = service.updateCurriculum(curriculum);
        //以上调用service更新课程

        return status > 0 ? "success" : "error";
    }

    /**
     * 根据课程类型查找对应课程
     *
     * @param resp                      应答对象
     * @param curriculumCharacteristics 课程类型
     * @param page                      页码
     * @param limit                     每页数量
     * @throws IOException
     */
    @GetMapping("/getCurriculumByCurriculumCharacteristics")
    @ResponseBody
    public void getCurriculumByCurriculumCharacteristics(HttpServletResponse resp, String curriculumCharacteristics, Integer page, Integer limit) throws IOException {
        System.out.println(curriculumCharacteristics);
        Page<Object> obj = PageHelper.startPage(page, limit);//分页
        List<CurriculumDto> curriculumList = service.queryCurriculumByCurriculumCharacteristics(curriculumCharacteristics);
        Integer count = service.countCurriculumByCurriculumCharacteristics(curriculumCharacteristics);
        //以上根据课程类型从service获取课程信息，curriculumList

        JsonUtils.customJson(resp, curriculumList, count);
    }

}
